package theeg.DependencyAnalysis.Core.Algorithms.SubstringDetection;

public class Edge {

    public int beginIndex;
    public int endIndex;
    public Node startNode;
    public Node endNode;

    public Edge(int beginIndex, int endIndex, Node startNode) {
        this.beginIndex = beginIndex;
        this.endIndex = endIndex;
        this.startNode = startNode;
        endNode = new Node(startNode.tree, null);
    }

    public Node splitEdge(Suffix suffix) {
        remove();
        Edge newEdge = new Edge(beginIndex, beginIndex + suffix.getSpan(), suffix.originNode);
        newEdge.insert();
        newEdge.endNode.suffixNode = suffix.originNode;
        beginIndex += suffix.getSpan() + 1;
        startNode = newEdge.endNode;
        insert();
        return newEdge.endNode;
    }

    public void insert() {
        startNode.addEdge(beginIndex, this);
    }

    public void remove() {
        startNode.removeEdge(beginIndex);
    }

    public int getSpan() {
        return endIndex - beginIndex;
    }
}
